<template>
	<view>
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
		</view>
		<!-- 搜索建议 -->
		<view class="sugg-list" v-if="searchResults.length !== 0">
			<view class="sugg-item" v-for="(item,i) in searchResults" :key="i">
				<view class="goods-name" @click="gotoGoodsdetail(item)">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="cleanHistory"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag v-for="(item, i) in historys" :key="i" :text="item" @click="gotoGoodsList(item)">
				</uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: "null",
				word: '',
				searchResults: [],
				// 搜索关键词的历史记录
				historyList: ['女装', '鞋子', '冬装']
			};
		},
		onLoad() {
			//在 onLoad 生命周期函数中，加载本地存储的搜索历史记录
			this.historyList = JSON.parse(uni.getStorageSync('word') || '[]')
		},
		methods: {
			//输入防抖的方法
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.word = e.trim()
					// console.log(this.word)
					this.getSearchList(this.word)
				}, 500)
			},
			//获取搜索建议的方法
			async getSearchList(word) {
				if (this.word === '') {
					this.searchResults = []
					return
				}
				const {
					data: res
				} = await uni.$http.get('searchlist1?q=' + word)
				// console.log(res)
				this.searchResults = res
				this.saveSearchHistory()
			},
			//将输入过的搜索建议放进搜索历史的方法
			saveSearchHistory() {
				//将搜索历史放进数组前面
				this.historyList.unshift(this.word)
				//解决关键词重复的问题（Set 对象）
				const set = new Set(this.historyList)
				 // console.log(set)
				set.delete(this.word)
				set.add(this.word)
				this.historyList = Array.from(set)
				//将搜索历史记录持久化存储到本地
				uni.setStorageSync('word', JSON.stringify(this.historyList))
				//数组中只存放10个历史记录，超过10个则删除最后一个
				if (this.historyList.length > 10) {
					this.historyList.splice(10, 1)
					uni.setStorageSync('word', JSON.stringify(this.historyList))
				}
			},
			//点击清空历史记录的方法
			cleanHistory() {
				// 清空 data 中保存的搜索历史
				this.historyList = []
				// 清空本地存储中记录的搜索历史
				uni.setStorageSync('word', '[]')
			},
			//点击搜索历史标签跳转页面的方法
			gotoGoodsList(word) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + word
				})
			},
			gotoGoodsdetail(item){
				// console.log(item)
				const id = item.goods_id
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id='+id
				})
			}

		},
		computed: {
			//将输入的historyList反转，使最新数据排在前面
			historys() {
				return [...this.historyList].reverse()
			}
		}
	}
</script>

<style lang="less">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}

	.history-box {
		padding: 0 5px;

		.history-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 40px;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}

		.history-list {
			display: flex;
			flex-wrap: wrap;

			.uni-tag {
				margin-top: 5px;
				margin-right: 5px;

			}
		}
	}

	.sugg-list {
		padding: 0 15px;

		.sugg-item {
			font-size: 12px;
			padding: 13px 5px;
			border-bottom: 1px solid #efefef;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.goods-name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}
</style>
